package com.android.lazylist;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.interfata.InterfActivity;
import com.android.interfata.Product1;
import com.android.interfata.R;
import com.android.interfata.XMLfunctions;

public class LazyAdapter extends BaseAdapter {
    
    private Activity activity;

    private int nr_act;
	public String[] title;
	public String[] detail;	
	public String[] imgid; 	
	public Double[] rating, price;	
	public String[] authors, brand;	
	public Long[] id;	
	public String[] imageLarge;	
	public String[] linkAmazon, linkEbay; 
	
	public int scrHeight, scrWidth;
	
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
    
    public LazyAdapter(Activity a, String[] img, String[] titl, String[] det, Double[]rat, Double[] prc, String[] imgL, 
    		String[] linkA, String[] linkE, Long[] d, String[] aut, String[] brd, int sH, int sW) 
    {
        activity = a;

        Log.d("act name", activity.getComponentName().toString());
        imgid = img;
        title = titl;
        detail = det;
        rating = rat;
        price = prc;
        imageLarge = imgL;
        linkAmazon = linkA;
        linkEbay = linkE;
        id = d;
        authors = aut;
        brand = brd;
        
        if (activity.getComponentName().toString().contains("Bookmark"))
        	nr_act = 2;
        else if (activity.getComponentName().toString().contains("Subscribe"))
        	nr_act = 1;
        
        scrHeight = sH;
        scrWidth = sW; 
        
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader = new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() 
    {
        return imgid.length;
    }

    public Object getItem(int position) 
    {
        return position;
    }

    public long getItemId(int position) 
    {
        return position;
    }
    
    public View getView(final int position, View convertView, ViewGroup parent) 
    {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.item, null);
        
        if (title[position].equals("-"))
        {
        	TextView text=(TextView)vi.findViewById(R.id.title);
        	if (nr_act == 1)
        		text.setText("You have no alarms yet!");
        	else
    	        text.setText("You have no bookmarks yes!");
	        
	        Button del = (Button) vi.findViewById(R.id.btnDel);
	        del.setVisibility(View.INVISIBLE);
	        
	        ImageView image=(ImageView)vi.findViewById(R.id.image);
	        Drawable img = (Drawable) activity.getResources().getDrawable(R.drawable.sad_face);
	        image.setImageDrawable(img);
        }
        else
        {
	        TextView text=(TextView)vi.findViewById(R.id.title);
	        text.setText(title[position]);
	        text.setOnClickListener(new Button.OnClickListener() 
			{
	            public void onClick(View v) 
	            {
	           	 	startProductActivity(position);
	            }
	        });
	        
	        TextView det = (TextView)vi.findViewById(R.id.detail);
	        det.setText(detail[position]);
	        det.setOnClickListener(new Button.OnClickListener() 
			{
	            public void onClick(View v) 
	            {
	           	 	startProductActivity(position);
	            }
	        });
	        
	        Button del = (Button) vi.findViewById(R.id.btnDel);
	        del.setOnClickListener(new Button.OnClickListener() 
			{
	            public void onClick(View v) 
	            {
	            	if (nr_act == 2)
	            	{
		            	stergeBookmark(id[position]);            	
		            	reloadBookmark();
	            	}
	            	else if (nr_act == 1)
	            	{
		            	stergeAlarma(id[position]);            	
		            	reloadAlarma();
	            	}
	            }
	        });
	        
	        ImageView image=(ImageView)vi.findViewById(R.id.image);
	        image.setOnClickListener(new Button.OnClickListener() 
			{
	            public void onClick(View v) 
	            {
	           	 	startProductActivity(position);
	           	 	
	            }
	        });        
	        imageLoader.DisplayImage(imgid[position], image, scrHeight, scrWidth);
        }
        return vi;
    }
    
    
    /* sterg bookmark */
    private void stergeBookmark(long idProd)
	{
		try
    	{
    		// preiau datele din SharedPref
    		SharedPreferences logPref = activity.getSharedPreferences("LoginPref", activity.MODE_PRIVATE);
	    	String idUser = logPref.getInt("ID", 0) + "";
	    	String user = logPref.getString("user", "username");
	    	String pass = logPref.getString("pass", "pass");
	    	
	    	String xml = XMLfunctions.getXML("RemoveUserBookmark?UserID=" + idUser + "&UserUsername=" + user + "&Password=" + pass 
	    				+ "&ProductID=" + idProd);
	    	Log.d("xml", xml);
	    	
	    	Document doc = XMLfunctions.XMLfromString(xml);
	    	NodeList nodes = doc.getElementsByTagName("Mesaje");
	        
	        Element e = (Element)nodes.item(0);
			
	        String mesaj = XMLfunctions.getValue(e, "Mesaj");

           	Toast toast = Toast.makeText(activity.getApplicationContext(), mesaj, Toast.LENGTH_SHORT);
           	toast.show();
    	}
    	catch( Exception e)
    	{
    		Log.d("exc", e.getMessage());
    	}
	}
    
    public void reloadBookmark() 
    {
        //Intent intent = activity.getIntent();
    	Intent intent = new Intent(activity.getApplicationContext(), InterfActivity.class);
    	intent.putExtra("activity", 2);
        activity.overridePendingTransition(0, 0);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        activity.finish();

        activity.overridePendingTransition(0, 0);
        activity.startActivity(intent);
    }
    /* end sterg bookmark */
    
    
    /* sterg alarma */
    private void stergeAlarma(long idProd)
	{
		try
    	{
    		// preiau datele din SharedPref
    		SharedPreferences logPref = activity.getSharedPreferences("LoginPref", activity.MODE_PRIVATE);
	    	String idUser = logPref.getInt("ID", 0) + "";
	    	String user = logPref.getString("user", "username");
	    	String pass = logPref.getString("pass", "pass");
	    	
	    	String xml = XMLfunctions.getXML("RemoveUserAlarm?UserID=" + idUser + "&UserUsername=" + user + "&Password=" + pass 
	    				+ "&ProductID=" + idProd);
	    	Log.d("xml", xml);
	    	
	    	Document doc = XMLfunctions.XMLfromString(xml);
	    	NodeList nodes = doc.getElementsByTagName("Mesaje");
	        
	        Element e = (Element)nodes.item(0);
			
	        String mesaj = XMLfunctions.getValue(e, "Mesaj");

           	Toast toast = Toast.makeText(activity.getApplicationContext(), mesaj, Toast.LENGTH_SHORT);
           	toast.show();
    	}
    	catch( Exception e)
    	{
    		Log.d("exc", e.getMessage());
    	}
	}
    
    public void reloadAlarma() 
    {
        //Intent intent = activity.getIntent();
    	Intent intent = new Intent(activity.getApplicationContext(), InterfActivity.class);
    	intent.putExtra("activity", 1);
        activity.overridePendingTransition(0, 0);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        activity.finish();

        activity.overridePendingTransition(0, 0);
        activity.startActivity(intent);
    }
    /* end sterg alarma */
    
    
	public void startProductActivity(int position)
	{
		// Launching new Activity on selecting single List Item
		Intent i = new Intent(activity.getApplicationContext(), Product1.class);
		// sending data to new activity

		i.putExtra("product", title[position]);
		i.putExtra("category", detail[position]);
		i.putExtra("rating", rating[position]);
		i.putExtra("price", price[position]);
		i.putExtra("authors", authors[position]);
		i.putExtra("linkAmazon", linkAmazon[position]);
		i.putExtra("linkEbay", linkEbay[position]);
		i.putExtra("id", id[position]);
		i.putExtra("brand", brand[position]);
		i.putExtra("imageLarge", imageLarge[position]);
		
		activity.startActivity(i);
	}
}